<?php
namespace Common\Logic\Admin;

use Common\Base\LogicBase;
use Common\Model\Admin\GroupModel;
use Common\Logic\Admin\NodeLogic;

class GroupLogic extends LogicBase
{
	public $m_group;

	public function __construct()
	{
		$this->m_group = new GroupModel();
	}

	/**
     * 得到分组列表
     *
     * @param int $w
     * @param array $search
     * @param string $fields
     * @param int $page
     * @param int $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getGroupList($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='', $exclude='')
    {
        return $this->m_group->getGroupList($w, $search, $fields, $page, $pageSize, $by, $exclude);
    }

    /**
     * 添加分组
     *
     * @param array $datas
     *
     * @return array
     */
    public function addGroup($datas)
    {
        if(isset($datas['group_name']) && $datas['group_name']!=''){
            $group_name = $datas['group_name'];
        }else{
            $this->setError('请输入分组名称');
            return false;
        }

        if(isset($datas['group_id']) && intval($datas['group_id'])>0){
            $group_pid = intval($datas['group_id']);
        }else{
            $group_pid = 0;
        }

        $group_acl = '';
        if(is_array($datas['auth']) && count($datas['auth'])){
            $tmp = array();
            foreach($datas['auth'] as $v){
                $v = intval($v);
                if($v > 0){
                    $tmp[] = $v;
                }
            }
            if(count($tmp)){
                $group_acl = implode(',', $tmp);
            }
        }

        $group_path = ',0,';
        $group_depth = '0';
        if($group_pid > 0){
            $up = $this->getGroupList(4, array('group_id'=>$group_pid), 'group_path');
            if($up){
                $group_path = $up['group_path'].$group_pid.',';
                $group_depth = substr_count($group_path, ',') - 2;
            }
        }

        $row = array();
        $row['group_name'] = $group_name;
        $row['group_pid'] = $group_pid;
        $row['group_path'] = $group_path;
        $row['group_depth'] = $group_depth;
        $row['group_acl'] = $group_acl;
        $row['st'] = 1;

        //检测是否重复
        $ck = $this->getGroupList(1, array('group_name'=>$group_name));
        if($ck > 0){ $this->setError('分组名称重复'); return false; }

        $insertID = $this->m_group->addGroup($row);
        if(false === $insertID){
            $this->setError($this->m_group->getError());
            return false;
        }else{
            return $insertID;
        }
    }

    /**
     * 得到权限的checkbox
     *
     * @param int     $group_id 分组id
     * @param int     $admin_id 用户id
     * @param boolean $is_group_page
     *
     * @return string
     */
    public function getNodesToCheckbox($group_id=0, $admin_id=0, $is_group_page=false)
    {
    	$L_node = new NodeLogic();
        $group_id = intval($group_id);
        $admin_id = intval($admin_id);
        $group_acl = $this->getGroupList(4, array('group_id'=>$group_id), 'group_acl');
        if(empty($group_acl)){ $group_acl = ''; }
        else{ $group_acl = trim($group_acl['group_acl']); }

        $nodes = $L_node->getNodeList(2, array('node_ids'=>($is_group_page?'':$group_acl)));

        $chk_nodes = array();

        if($is_group_page && $group_acl!=''){
            $chk_nodes = explode(',', $group_acl);
        }
        
        $L_admin = new AdminLogic();
        if($admin_id !== 0){
            $admin_acl = $L_admin->getAdminList(4, array('admin_id'=>$admin_id), 'a.admin_acl');
            $chk_nodes = explode(',', $admin_acl['admin_acl']);
        }

        if( ! empty($nodes)){
            $nodes = list_to_tree($nodes, 'node_id', 'node_pid', '_sub');
            $html = '';
            foreach($nodes as $v){
                if($admin_id>0 || $is_group_page){$ck = in_array($v['node_id'], $chk_nodes) ? 'checked="checked"' : '';}
                $html .= '<div><input class="cb" type="checkbox" name="auth[]" id="n_'.$v['node_id'].'" value="'.$v['node_id'].'" '.$ck.'><label for="n_'.$v['node_id'].'" class="color_0 fb f14">'.$v['node_name'].'</label>';
                if($v['_sub'])foreach($v['_sub'] as $vv){
                    if($admin_id>0 || $is_group_page){$ck = in_array($vv['node_id'], $chk_nodes) ? 'checked="checked"' : '';}
                    $html .= '<div class="mleft30px"><input class="cb" type="checkbox" name="auth[]" id="n_'.$vv['node_id'].'" value="'.$vv['node_id'].'" '.$ck.' /><label for="n_'.$vv['node_id'].'" class="fb">'.$vv['node_name'].'</label>
                            <div class="mleft60px bd1sccc">';
                    if($vv['_sub'])foreach($vv['_sub'] as $vvv){
                        if($admin_id>0 || $is_group_page){$ck = in_array($vvv['node_id'], $chk_nodes) ? 'checked="checked"' : '';}
                        $html .= '<input type="checkbox" name="auth[]" id="n_'.$vvv['node_id'].'" value="'.$vvv['node_id'].'" '.$ck.' /><label for="n_'.$vvv['node_id'].'">'.$vvv['node_name'].'</label>&nbsp;&nbsp;&nbsp;';
                    }
                    $html .= '</div>
                        </div>';
                }
                $html .= '</div>';
            }
            return $html;
        }else{
            return '';
        }
    }

    /**
     * 得到分组select
     *
     * @param int    $choose 默认选中
     * @param string $name 名称、ID
     * @param string $onchange 事件
     * @param boolean $disabled
     *
     * @return string
     */
    public function getGroupSelect($choose=-1, $name='group_id', $onchange='', $disabled=false)
    {
        $dis = $disabled ? 'disabled="disabled"' : '';
        $html = '<select name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'" '.$dis.'><option value="-1">--请选择--</option>';
        $list = $this->getGroupList(2, array('st'=>1), 'group_id,group_pid,group_name,group_depth');
        if( ! empty($list)){
            $choose = intval($choose);
            $list = list_to_tree($list, 'group_id', 'group_pid', '_sub');
            $rs = array();
            floatArray($list, '_sub', $rs);
            unset($list);
            foreach($rs as $v){
                $sel = (intval($v['group_id']) === $choose) ? 'selected="selected"' : '';
                $html .= '<option value="'.$v['group_id'].'" '.$sel.'>'.str_repeat('&nbsp;', $v['group_depth']*6).$v['group_name'].'</option>';
            }
        }
        $html .= '</select>';
        return $html;
    }

    /**
     * 修改分组
     *
     * @param array $datas
     *
     * @return array
     */
    public function editGroup($datas = array())
    {
        if(isset($datas['group_id']) && intval($datas['group_id'])>0){
            $group_id = intval($datas['group_id']);
        }else{
            $this->setError('参数错误'); return false;
        }
        if(isset($datas['group_name']) && $datas['group_name']!=''){
            $group_name = $datas['group_name'];
        }else{
            $this->setError('请输入分组名称'); return false;
        }

        if(isset($datas['group_pid']) && intval($datas['group_pid'])>0){
            $group_pid = intval($datas['group_pid']);
        }else{
            $group_pid = 0;
        }
        if($datas['group_id'] == $datas['group_pid']){ $this->setError('所属分组不能是自己'); return false; }

        $group_acl = '';
        if(is_array($datas['auth']) && count($datas['auth'])){
            $tmp = array();
            foreach($datas['auth'] as $v){
                $v = intval($v);
                if($v > 0){
                    $tmp[] = $v;
                }
            }
            if(count($tmp)){
                $group_acl = implode(',', $tmp);
            }
        }

        $group_path = ',0,';
        $group_depth = '0';
        if($group_pid > 0){
            $up = $this->getGroupList(4, array('group_id'=>$group_pid), 'group_path');
            if($up){
                $group_path = $up['group_path'].$group_pid.',';
                $group_depth = substr_count($group_path, ',') - 2;
            }
        }

        $row = array();
        $row['group_name'] = $group_name;
        $row['group_pid'] = $group_pid;
        $row['group_path'] = $group_path;
        $row['group_depth'] = $group_depth;
        $row['group_acl'] = $group_acl;

        $aff = $this->updateGroupByGroupId($row, $group_id);
        if(false === $aff){
            $this->setError($this->m_group->getError());
            return false;
        }else{
            return $aff;
        }
    }

    /**
     * 修改分组
     *
     * @param array $dat 需要更新的数据
     * @param int   $group_id
     *
     * @return array
     */
    public function updateGroupByGroupId($dat=array(), $group_id=0)
    {
        $aff = $this->m_group->updateGroupByGroupId($dat, $group_id);
        if(false === $aff){
            $this->setError($this->m_group->getError());
            return false;
        }else{
            return $aff;
        }
    }

    /**
     * 删除分组
     *
     * @param int $group_id
     *
     * @return array
     */
    public function delGroup($group_id = 0)
    {
    	$L_admin = new AdminLogic();
        $group_id = intval($group_id); if($group_id < 1){ $this->setError('参数错误'); return false; }
        //检查有没有子分组
        $ck = $this->getGroupList(1, array('group_path'=>','.$group_id.','));
        if($ck > 0){ $this->setError('有子分组，不能删除'); return false; }
        //检查有没有用户
        $ck = $L_admin->getTableAdmin(1, array('group_id'=>$group_id));
        if($ck > 0){ $this->setError('分组下有用户，不能删除'); return false; }
        $aff = $this->m_group->delGroup($group_id);
        if(false === $aff){
            $this->setError($this->m_group->getError());
            return false;
        }else{
            return $aff;
        }
    }

}